<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="../js/jquery-min-2.1.3.js" ></script>
    <script type="text/javascript" src="../js/jquery.class.min.js" ></script>
    <script type="text/javascript" src="../js/md5-min.js" ></script>
    <script type="text/javascript" src="../js/digest-auth.js" ></script>
    <script type="application/javascript">
        var digestAuth = new pl.arrowgroup.DigestAuthentication(
                {
                    onSuccess : function(response){
                        var result = eval("("+response+")");
                        alert(response);
                    },
                    onFailure : function(response){
                        alert("Faliure:"+eval("("+response+")"));
                    },
                    cnonce : 'testCnonce'
                }
        );
        digestAuth.setCredentials("chuanbao","123456");

        var TestJs = {
            sendRequest: function(type,url,data){
                var xhr = (window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'));
                xhr.open(type, url, false);
                xhr.setRequestHeader("Content-Type", "application/json");
                xhr.setRequestHeader("Accept", "application/json");
                xhr.send(JSON.stringify(data));
                return xhr;
            },
            save: function(){
                var str = window.prompt("请输入用户名和密码(username:password)");
                var user = str.split(":");
                var data = {
                    username:user[0],
                    password:user[1]
                };
                digestAuth.HTTP_METHOD="post";
//                digestAuth.DATA = data;
                digestAuth.call("/api/v1/user");
//
//                var xhr = this.sendRequest("post","/api/v1/user",data);
//                if (xhr.readyState == 4 && xhr.status == 200) {
//                    var result = eval("("+xhr.responseText+")");
//                    alert("User created(username:"+result.username+",password:"+result.password+")");
//                }
            },
            update: function(){
                var str = window.prompt("请输入你的新密码");
                var user = {
                    password: str
                };
                var xhr = this.sendRequest("put","/api/v1/user",user);
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var result = xhr.responseText;
                    alert("The new password:"+eval("("+result+")").password);
                }
            },
            deleteUser: function(){
                var str = window.prompt("请输入你要删除的用户：");
                var xhr = this.sendRequest("delete","/api/v1/user/"+str,null);
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var result = eval("("+xhr.responseText+")");
                    alert("Delete user(username:"+result.username);
                }
            },
            getUser: function(){
                var str = window.prompt("请输入你要查找的用户名称：");
                var xhr = this.sendRequest("get","/api/v1/user/"+str,null);
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var result = eval("("+xhr.responseText+")");
                    alert("Get user(username:"+result.username+",password:"+result.password+")");
                }
            }

        }
    </script>
</head>
<body>
    Hello test page!!
    <div style="margin-top: 40px;"></div>
<a href="javascript:void(0);" onclick="TestJs.save();">Save</a>
    <div style="margin-top: 20px;"></div>
<a href="javascript:void(0);" onclick="TestJs.update();">Update</a>
    <div style="margin-top: 20px;"></div>
    <a href="javascript:void(0);" onclick="TestJs.deleteUser();">Delete</a>
    <div style="margin-top: 20px;"></div>
    <a href="javascript:void(0);" onclick="TestJs.getUser();">Get</a>

    <div style="margin-top: 20px;"></div>
    <a href="/j_spring_security_logout">Logout</a>
</body>
</html>
